import java.net.InetAddress;

/**
 * Record necessary information of every member in a chat group 
 * @author weizhuowu
 *
 */
public class UDPNode {

	private String username;
	private InetAddress localAddr;
	private int localPortNum;
	private int aliveTry;
	private boolean isLeader;
    /**
     * constructor for normal members in chat group
     * @param name
     * @param localAddr
     * @param portNum
     */
	UDPNode(String name, InetAddress localAddr, int portNum){
		this.username = name;
		this.localAddr = localAddr;
		this.localPortNum = portNum;
		this.isLeader = false;
		this.aliveTry = 3;
	}
	/**
	 * constructor for chat group leader	 
	 */
	UDPNode(String name, InetAddress localAddr, int portNum, boolean isLeader){
		this.username = name;
		this.localAddr = localAddr;
		this.localPortNum = portNum;
		this.isLeader = isLeader;
		this.aliveTry = 3;
	}

	@Override
	public String toString(){
		return "IP:"+this.localAddr.getHostAddress()+":PORT:"+this.localPortNum+":NAME:"+this.username+":ISLEADER:"+this.isLeader+";";
	}
	@Override
	public boolean equals(Object node){
		if(node.getClass().equals(UDPNode.class)){
			boolean IpPortNameEqual = ((UDPNode)node).toString().equals(this.toString());
			boolean aliveEqual = ((UDPNode) node).getAliveTry()==this.aliveTry;
			boolean roleEqual = ((UDPNode) node).isLeader()==this.isLeader;
			return IpPortNameEqual&&aliveEqual&&roleEqual;
		}else{
			return false;
		}
	}

	public String getUsername(){
		return this.username;
	}
	public InetAddress getIpAddress(){
		return this.localAddr;
	}
	public int getPortNum(){
		return this.localPortNum;
	}
	public void decreaseAlive(){
		this.aliveTry-=1;
	}
	public int getAliveTry(){
		return this.aliveTry;
	}	
	public String getIpPort(){
		return "IP:"+this.localAddr+":PORT:"+this.localPortNum;
	}
	public boolean isLeader(){
		return this.isLeader;
	}
}
